package com.flyou.ccnucenter.ui.activity.mine;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.ui.activity.account.MyAccountActivity;
import com.flyou.ccnucenter.ui.activity.feedBack.FeedBackActivity;
import com.flyou.ccnucenter.ui.activity.login.LoginActivity;
import com.flyou.ccnucenter.ui.activity.setting.SettingActivity;
import com.flyou.ccnucenter.utils.SPUtils;

import org.simple.eventbus.Subscriber;


public class PersonalCenterActivity extends BaseActivity1 implements View.OnClickListener {

    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private com.facebook.drawee.view.SimpleDraweeView userheaderbg;
    private android.widget.TextView userName;
    private android.widget.TextView textViewpostlist;
    private android.widget.TextView textViewmyreplypost;
    private android.widget.TextView textViewmyreport;
    private android.widget.TextView textViewmessage;
    private android.widget.TextView textViewaccount;
    private android.widget.TextView textViewfeedback;
    private android.widget.TextView textViewsetting;


    @Override
    protected void initView() {
        setContentView(R.layout.activity_personal_center);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);


        this.textViewsetting = (TextView) findViewById(R.id.textView_setting);
        this.textViewfeedback = (TextView) findViewById(R.id.textView_feedback);
        this.textViewaccount = (TextView) findViewById(R.id.textView_account);
        this.textViewmessage = (TextView) findViewById(R.id.textView_message);
        this.textViewmyreport = (TextView) findViewById(R.id.textView_my_report);

        this.textViewmyreplypost = (TextView) findViewById(R.id.textView_my_reply_post);
        this.textViewpostlist = (TextView) findViewById(R.id.textView_post_list);
        this.userName = (TextView) findViewById(R.id.userName);
        this.userheaderbg = (SimpleDraweeView) findViewById(R.id.user_header_bg);


    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("个人中心");
        SetUserInfo();
    }

    /*
    * 设置用户信息*/
    private void SetUserInfo() {
        String headImage = (String) SPUtils.get(PersonalCenterActivity.this, "headImage", "");
        String userName = (String) SPUtils.get(PersonalCenterActivity.this, "userName", "");
        if (!TextUtils.isEmpty(headImage)) {
            userheaderbg.setImageURI(Uri.parse(headImage));
        } else {
            userheaderbg.setImageURI(Uri.parse(""));
        }
        if (!TextUtils.isEmpty(userName)) {
            this.userName.setText(userName);
        } else {
            this.userName.setText("点击登录");
        }
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        userheaderbg.setOnClickListener(this);
        textViewsetting.setOnClickListener(this);
        textViewfeedback.setOnClickListener(this);
        textViewaccount.setOnClickListener(this);
        textViewmessage.setOnClickListener(this);
        textViewmyreport.setOnClickListener(this);
        textViewmyreplypost.setOnClickListener(this);
        textViewmyreplypost.setOnClickListener(this);
        textViewpostlist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                finish();
                break;
            case R.id.user_header_bg:
                if (isLogin()) {
                    openMyAccountActivity();
                }
                break;
            case R.id.textView_account:
                if (isLogin()) {
                    openMyAccountActivity();
                }
                break;
            case R.id.textView_setting:
                Intent settingIntent = new Intent(PersonalCenterActivity.this, SettingActivity.class);
                startActivity(settingIntent);
                break; case R.id.textView_feedback:
                if (isLogin()){
                Intent feedBackIntent = new Intent(PersonalCenterActivity.this, FeedBackActivity.class);
                startActivity(feedBackIntent);}
                break;
            case R.id.textView_post_list:
                    if (isLogin()){
                        Intent myLostIntent =new Intent(PersonalCenterActivity.this,MyLostActivity.class);
                        startActivity(myLostIntent);
                    }
                break;
            case R.id.textView_my_reply_post :

                if (isLogin()){
                    Intent myLoveIntent =new Intent(PersonalCenterActivity.this,MyLoveActivity.class);
                    startActivity(myLoveIntent);
                }
                break;
        }

    }

    private void openMyAccountActivity() {
        Intent intent = new Intent(PersonalCenterActivity.this, MyAccountActivity.class);
        startActivity(intent);
    }

    public boolean isLogin(){
        String userName = (String) SPUtils.get(PersonalCenterActivity.this, "userName", "");
        if (TextUtils.isEmpty(userName)) {

            Intent intent =new Intent(PersonalCenterActivity.this,LoginActivity.class);
            startActivity(intent);
            return false;
        }
        else {

            return true;
        }
    }

    @Subscriber(tag = Constant.UPDATE_HEADIMAGE)
    public void updateImage(String tag) {

        SetUserInfo();

    }
}
